import React, { PureComponent } from 'react'
import { EventEmitter } from 'events'
// 1.创建事件总线
const EventBus = new EventEmitter()

export class Test extends PureComponent {
  render() {
    return (
      <>
        <div>Test</div>
        <hr />
        <TestA></TestA>
        <hr />
        <TestB></TestB>
      </>
    )
  }
}
export class TestA extends PureComponent {
  emeitEventA() {
    EventBus.emit('hello', 'hello react', 111)
  }
  render() {
    return (
      <>
        <button onClick={() => this.emeitEventA()}>TestA</button>
        <TestAson></TestAson>
      </>
    )
  }
}
export class TestB extends PureComponent {
  // 监听事件
  componentDidMount() {
    EventBus.addListener('hello', this.helloEvent)
  }
  helloEvent = (msg, num) => {
    console.log(msg, num)
  }
  // 取消监听事件
  componentWillUnmount() {
    EventBus.removeListener('hello', this.helloEvent)
  }
  render() {
    return <div>TestB</div>
  }
}
export class TestAson extends PureComponent {
  render() {
    return <div>TestAson</div>
  }
}
